Completed
Push — master ( 2980fc...0dcc55 )
by
unknown
02:11
created

duplicate.js ➔ ... ➔ ???   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 7
c 2
b 1
f 0
nc 8
dl 0
loc 43
rs 6.7272
nop 2
1
import path from 'path'
2
import extend from 'extend'
3
4
import {
5
  abeExtend,
6
  Manager,
7
  config,
8
  coreUtils,
9
  cmsData,
10
  cmsOperations
11
} from '../../'
12
13
const duplicate = function(oldPostUrl, template, newPath, name, req, isUpdate = false) {
14
  const p = new Promise((resolve, reject) => {
15
    abeExtend.hooks.instance.trigger('beforeDuplicate', oldPostUrl, template, newPath, name, req, isUpdate)
16
17
    let json = {}
18
    let revisions = []
19
    const newPostUrl = path.join(newPath, name)
20
    if(oldPostUrl != null) {
21
      const files = Manager.instance.getList()
22
      const oldPostDataPath = path.join(config.root, config.data.url, oldPostUrl.replace('.' + config.files.templates.extension, '.json'))
23
      let posts = []
0 ignored issues
show
Unused Code introduced by
The assignment to variable posts seems to be never used. Consider removing it.
Loading history...
24
      posts = coreUtils.array.filter(files, 'path', oldPostDataPath)
25
26
      if(posts.length > 0 && posts[0].revisions != null) {
27
        revisions = posts[0].revisions
28
        if(revisions != null && revisions[0] != null) {
29
          json = cmsData.file.get(revisions[0].path)
30
31
          json = extend(true, json, req.body)
32
          console.log('* * * * * * * * * * * * * * * * * * * * * * * * * * * * *')
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
33
          console.log('json', json)
34
35
          delete json.abe_meta
36
        }
37
      }
38
    }
39
40
    abeExtend.hooks.instance.trigger('afterDuplicate', json, oldPostUrl, template, newPath, name, req, isUpdate)
41
42
    var pCreate = cmsOperations.create(template, newPath, name, req, json, (isUpdate) ? false : true)
43
    pCreate.then((resSave) => {
44
      if (isUpdate && oldPostUrl !== newPostUrl) {
45
        abeExtend.hooks.instance.trigger('beforeUpdate', json, oldPostUrl, template, newPath, name, req, isUpdate)
46
        cmsOperations.remove.remove(oldPostUrl)
47
      }
48
      resolve(resSave)
49
    },
50
    () => {
51
      reject()
52
    }).catch(function(e) {
53
      console.error('[ERROR] abe-duplicate.js', e)
54
      reject()
55
    })
56
  })
57
58
  return p
59
}
60
61
export default duplicate